<?php
/**
 * Created by PhpStorm.
 * User: idcu
 * Date: 2016/3/2
 * Time: 9:19
 */
namespace Admin\Model;
use IDCU\Model\RelationModel;

class DictModel extends RelationModel
{
    protected $_validate = array(
        array('type','/^\w+$/','类型格式不正确',1,'regex',3)
    );

    public function regroup(){
        $data = $this->order('sort')->select();
        foreach($data as $v){
            $result[$v['type']][$v['value']] = $v;
        }
        S('dict',$result,3600);
        return S('dict');
    }

    public function getDict($type){
        if(!S('dict')){
            $result = $this->regroup()[$type];
        }else{
            $result = S('dict')[$type];
        }
        return $result;
    }

    public function dictTransform(&$resultSet,$dictTransform)
    {
        foreach($resultSet as $k1=>$v1){
            foreach($dictTransform as $k2=>$v2){
                if(array_key_exists($v2,$v1)){
                    $resultSet[$k1][$v2] = $this->getDict($v2)[$v1[$v2]]['text'];
                }
            }
        }
    }

    protected function _after_insert($data, $options)
    {
        parent::_after_insert($data, $options); // TODO: Change the autogenerated stub
        $dict = $this->regroup();
    }

    protected function _after_update($data, $options)
    {
        parent::_after_update($data, $options); // TODO: Change the autogenerated stub\
        $dict = $this->regroup();
    }
}